package com.cxf.windex.rest;

import com.cxf.windex.base.msg.ObjectRestResponse;
import com.cxf.windex.base.msg.TableResultResponse;
import com.cxf.windex.base.rest.BaseRestController;
import com.cxf.windex.base.util.JWDUtil;
import com.cxf.windex.biz.AddressBiz;
import com.cxf.windex.entity.Address;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author chenxf
 * @date 3/20/2018
 * @description
 */
@Api(tags = {"小程序 - 地址接口"})
@Controller
@RequestMapping("/user/address")
public class AddressController extends BaseRestController<AddressBiz, Address> {

  @ApiOperation(value = "查询地址", notes = "模糊查询地址")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "address", value = "地址", required = false, paramType = "query", dataType = "String"),
  })
  @RequestMapping(value = "/address", method = RequestMethod.GET)
  @ResponseBody
  public ObjectRestResponse<String> name(String address) {

    if (StringUtils.isBlank(address)) {
      return new ObjectRestResponse().rel(true).msg("请传入地址");
    }

    List<String> list = baseBiz.selectName(address);
    return new ObjectRestResponse().rel(true).result(list);
  }

  @Cacheable(value="addressList")
  @ApiOperation(value = "查询地图技师", notes = "查询地图技师列表")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "latitude", value = "中心点经度", required = false, paramType = "query", dataType = "String"),
      @ApiImplicitParam(name = "longitude", value = "中心点纬度", required = false, paramType = "query", dataType = "String"),
      @ApiImplicitParam(name = "latitudeE", value = "顶点经度", required = false, paramType = "query", dataType = "String"),
      @ApiImplicitParam(name = "longitudeE", value = "顶点纬度", required = false, paramType = "query", dataType = "String")
  })
  @RequestMapping(value = "/map", method = RequestMethod.GET)
  @ResponseBody
  public ObjectRestResponse<Address> map( String latitude,String longitude, String latitudeE,String longitudeE) {
    //TODO 暂时不作数量限制
    if (StringUtils.isBlank(latitude)||StringUtils.isBlank(longitude)||StringUtils.isBlank(latitudeE)||StringUtils.isBlank(longitudeE)) {
      return new ObjectRestResponse().rel(true).msg("请传入经纬度坐标");
    }

    List<Address> list = baseBiz.selectMap(latitude,longitude, JWDUtil.getStrDistance(longitude,latitude,longitudeE,latitudeE));
    return new ObjectRestResponse().rel(true).result(list);
  }
  @Cacheable(value="addressList")
  @ApiOperation(value = "根据地区查询技师列表", notes = "根据地区查询技师列表")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "limit", value = "记录数", required = false, paramType = "query", dataType = "int"),
      @ApiImplicitParam(name = "offset", value = "页码", required = false, paramType = "query", dataType = "int"),
      @ApiImplicitParam(name = "latitude", value = "中心点经度", required = false, paramType = "query", dataType = "String"),
      @ApiImplicitParam(name = "longitude", value = "中心点纬度", required = false, paramType = "query", dataType = "String"),
      @ApiImplicitParam(name = "address", value = "地区", required = false, paramType = "query", dataType = "String"),
      @ApiImplicitParam(name = "orderStr", value = "排序字段 如score", required = false, paramType = "query", dataType = "String")
  })
  @RequestMapping(value = "/list", method = RequestMethod.GET)
  @ResponseBody
  public TableResultResponse<Address> list(@RequestParam(defaultValue = "10") int limit, @RequestParam(defaultValue = "1") int offset,
                                           String latitude, String longitude, String address, String orderStr) {
    String order="";
    if(StringUtils.isNotBlank(orderStr)){
      order="jst."+orderStr+" desc ,";
    }

    PageHelper.startPage(offset, limit);

    List<Address> items = baseBiz.selectList(latitude,longitude,address,order);

    PageInfo<Address> pageInfo = new PageInfo<Address>(items);
    return new TableResultResponse<Address>(Integer.parseInt(String.valueOf(pageInfo.getTotal())), offset, limit,
        items);
  }
}
